package com.smart.village.big.screen.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.smart.village.big.screen.domain.*;
import com.smart.village.big.screen.mapper.PartyOrganizationActivityMapper;
import com.smart.village.big.screen.mapper.PartyOrganizationMapper;
import com.smart.village.big.screen.mapper.WorkTeamsMapper;
import com.smart.village.big.screen.service.PartyOrganizationActivityService;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class PartyOrganizationActivityServiceImpl implements PartyOrganizationActivityService {

    @Autowired
    private PartyOrganizationActivityMapper partyOrganizationActivityMapper;
    @Autowired
    private PartyOrganizationMapper partyOrganizationMapper;
    @Autowired
    private WorkTeamsMapper workTeamsMapper;

    @Override

    public JSONObject getPartyOrganizationActivityList(String formatDate, String organizationSerialNumber, List<String> activityTypeList, String pOrganizationType) {

            JSONObject resultJson = new JSONObject();
            HashMap<String, Object> param = Maps.newHashMap();
            // 输入 活动类型名称
            param.put("activityTypeList", activityTypeList);

            // 输入 筛选时间范围
            if (StringUtils.isNotBlank(formatDate)) {
                param.put("formatDate", formatDate);
            }

            // 输入筛选的党组织 树列表
            List<PartyOrganization> treePartyOrganizationList = Lists.newArrayList();
            PartyOrganization partyOrganization = partyOrganizationMapper.selectBySerialNumber(organizationSerialNumber);
            if (!ObjectUtils.isEmpty(partyOrganization)) {
                treePartyOrganizationList.add(partyOrganization);
            }
            List<PartyOrganization> partyOrganizations = partyOrganizationMapper.selectTreeListByParentSerialNumber(organizationSerialNumber, pOrganizationType);
            if (!ObjectUtils.isEmpty(partyOrganizations)) {
                treePartyOrganizationList.addAll(partyOrganizations);
            } else {
                resultJson.put("list", Lists.newArrayList());
                resultJson.put("should", 0);
                resultJson.put("plan", 0);
                resultJson.put("started", 0);
                resultJson.put("notStarted", 0);
                return resultJson;
            }
            ArrayList<String> treePartyOrganizationSerialNumberList = Lists.newArrayList();
            treePartyOrganizationList.forEach(o1 -> {
                if (o1.getPartyOrganizationType().equals("支部")) {
                    treePartyOrganizationSerialNumberList.add(o1.getPartyOrganizationSerialNumber());
                }
            });
            param.put("treePartyOrganizationSerialNumberList", treePartyOrganizationSerialNumberList);

            List<PartyOrganizationActivitiesStaticVo> partyOrganizationActivitiesStaticVos = partyOrganizationActivityMapper.selectPartyOrganizationActivitiesVoListByActivityType(param);
            resultJson.put("list", partyOrganizationActivitiesStaticVos);

            // 计算出当前大屏的 应开展 计划开展 已开展 未开展的 状态的数量
            long started = partyOrganizationActivitiesStaticVos.stream().filter(partyOrganizationActivitiesStaticVo -> partyOrganizationActivitiesStaticVo.getActivityStatus().equals("已开展")).count();
            long not_started = partyOrganizationActivitiesStaticVos.stream().filter(partyOrganizationActivitiesStaticVo -> partyOrganizationActivitiesStaticVo.getActivityStatus().equals("未开展")).count();
            int should = 0;
            for (String actType : activityTypeList) {
                for (PartyOrganization po : treePartyOrganizationList) {
                    String partyOrganizationType = po.getPartyOrganizationType();
                    JSONObject actTypeJson = new JSONObject();
                    actTypeJson.put("activityType", actType);
                    actTypeJson.put("partyOrganizationType", partyOrganizationType);
                    // 去 party_organization_activities_frequency 表中获取频次
                    // 1. year  固定 是 1倍的 Frequency
                    // 2. quarter  固定 是 1倍的 Frequency
                    // 3. month  固定 是 1倍的 Frequency
                    // 4. week  固定 是 5倍的 Frequency
                    List<PartyOrganizationActivitiesFrequency> partyOrganizationActivitiesFrequencies = partyOrganizationActivityMapper.selectPartyOrganizationActivitiesFrequencyByActivityTypeAndPartyOrganizationType(actTypeJson);

                    for (PartyOrganizationActivitiesFrequency partyOrganizationActivitiesFrequency : partyOrganizationActivitiesFrequencies) {
                        String frequency = partyOrganizationActivitiesFrequency.getFrequency();
                        if (StringUtils.isEmpty(frequency)) {
                            frequency = "0";
                        }
                        String cycle = partyOrganizationActivitiesFrequency.getCycle();
                        switch (cycle) {
                            case "year": {
                                // 一年完成frequency即可
                                should += Integer.parseInt(frequency);
                                break;
                            }
                            case "quarter": {
                                // 一季度完成frequency即可
                                should += Integer.parseInt(frequency);
                                break;
                            }
                            case "month": {
                                // 每个月完成frequency次
                                should += Integer.parseInt(frequency);
                                break;
                            }
                            case "week": {
                                // 一周一次 每个月 5*frequency次
                                should += Integer.parseInt(frequency) * 5;
                                break;
                            }
                            default: {
                                break;
                            }
                        }
                    }
                }
            }
            resultJson.put("should", should);
            resultJson.put("plan", partyOrganizationActivitiesStaticVos.size());
            resultJson.put("started", started);
            resultJson.put("notStarted", not_started);

            return resultJson;

    }

    @Override
    public JSONObject getTwoContactTwoReport(String formatDate, String organizationSerialNumber, List<String> activityTypeList) {
        JSONObject resultJson = new JSONObject();
        HashMap<String, Object> param = Maps.newHashMap();
        // 输入 活动类型名称
        param.put("activityTypeList", activityTypeList);

        // 输入 筛选时间范围
        if (StringUtils.isNotBlank(formatDate)) {
            param.put("formatDate", formatDate);
        }

        // 输入筛选的党组织 树列表
        List<PartyOrganization> treePartyOrganizationList = Lists.newArrayList();
        PartyOrganization partyOrganization = partyOrganizationMapper.selectBySerialNumber(organizationSerialNumber);
        if (!ObjectUtils.isEmpty(partyOrganization)) {
            treePartyOrganizationList.add(partyOrganization);
        }
        List<PartyOrganization> partyOrganizations = partyOrganizationMapper.selectTreeListByParentSerialNumber(organizationSerialNumber, "支部");
        if (!ObjectUtils.isEmpty(partyOrganizations)) {
            treePartyOrganizationList.addAll(partyOrganizations);
        }
        ArrayList<String> treePartyOrganizationSerialNumberList = Lists.newArrayList();
        treePartyOrganizationList.forEach(o1 -> {
            if (o1.getPartyOrganizationType().equals("支部")) {
                treePartyOrganizationSerialNumberList.add(o1.getPartyOrganizationSerialNumber());
            }
        });
        param.put("treePartyOrganizationSerialNumberList", treePartyOrganizationSerialNumberList);

        List<PartyOrganizationActivitiesStaticVo> partyOrganizationActivitiesStaticVos = partyOrganizationActivityMapper.selectPartyOrganizationActivitiesVoListByActivityType(param);
        resultJson.put("list", partyOrganizationActivitiesStaticVos);
        // 共开展
        resultJson.put("total", partyOrganizationActivitiesStaticVos.size());
        long signUp;
        long finish;

        long started = partyOrganizationActivitiesStaticVos.stream().filter(partyOrganizationActivitiesStaticVo -> partyOrganizationActivitiesStaticVo.getActivityStatus().equals("已开展")).count();
        finish = partyOrganizationActivitiesStaticVos.stream().filter(partyOrganizationActivitiesStaticVo -> partyOrganizationActivitiesStaticVo.getActivityStatus().equals("已完成")).count();
        //总报名活动数量 =  已开展的活动数量 + 已完成的数量
        signUp = started + finish;
        resultJson.put("signUp", signUp);
        // 共参与 为有人签到的活动数量
        resultJson.put("participateIn", started);
        resultJson.put("finish", finish);
        return resultJson;
    }

    @Override
    public JSONObject getEarlyWarning(String formatDate, String organizationSerialNumber, List<String> activityTypeList) {
        JSONObject jsonObject = new JSONObject();
        List<String> nameList = new ArrayList<>();
        nameList.add("已报到");
        nameList.add("超时报到");
        nameList.add("未报到");
        jsonObject.put("nameList", nameList);
        List<Integer> dataList = new ArrayList<>();
        int signUpCount = 0;
        int timeOutCount = 0;
        int noSignUpCount = 0;
        HashMap<String, Object> param = Maps.newHashMap();
        // 输入 活动类型名称
        param.put("activityTypeList", activityTypeList);

        // 输入 筛选时间范围
        if (StringUtils.isNotBlank(formatDate)) {
            param.put("formatDate", formatDate);
        }
        // 输入筛选的党组织 树列表
        List<PartyOrganization> treePartyOrganizationList = Lists.newArrayList();
        PartyOrganization partyOrganization = partyOrganizationMapper.selectBySerialNumber(organizationSerialNumber);
        if (!ObjectUtils.isEmpty(partyOrganization)) {
            treePartyOrganizationList.add(partyOrganization);
        }
        List<PartyOrganization> partyOrganizations = partyOrganizationMapper.selectTreeListByParentSerialNumber(organizationSerialNumber, "支部");
        if (!ObjectUtils.isEmpty(partyOrganizations)) {
            treePartyOrganizationList.addAll(partyOrganizations);
        }
        ArrayList<String> treePartyOrganizationSerialNumberList = Lists.newArrayList();
        treePartyOrganizationList.forEach(o1 -> {
            if (o1.getPartyOrganizationType().equals("支部")) {
                treePartyOrganizationSerialNumberList.add(o1.getPartyOrganizationSerialNumber());
            }
        });
        param.put("treePartyOrganizationSerialNumberList", treePartyOrganizationSerialNumberList);

        List<HashMap> earlyWarning = partyOrganizationActivityMapper.getEarlyWarning(param);
        for (HashMap m : earlyWarning) {
            if (m.get("checkStatus").toString().equals("已报到")) {
                signUpCount += Integer.valueOf(m.get("cnt").toString());
            }
            if (m.get("checkStatus").toString().equals("超时报到")) {
                timeOutCount += Integer.valueOf(m.get("cnt").toString());
            }
            if (m.get("checkStatus").toString().equals("未报到")) {
                noSignUpCount += Integer.valueOf(m.get("cnt").toString());
            }
        }
        dataList.add(signUpCount);
        dataList.add(timeOutCount);
        dataList.add(noSignUpCount);
        jsonObject.put("dataList", dataList);
        return jsonObject;
    }

    @Override
    public JSONObject getMicroPartyOrganizationActivityListByStatus(
            String formatDate,
            String partyOrganizationSerialNumber,
            List<String> activityTypeList,
            String activityStatus,
            String partyOrganizationType,
            int pageNum,
            int pageSize) {
        HashMap<String, Object> param = Maps.newHashMap();
        // 输入 活动类型名称
        param.put("activityTypeList", activityTypeList);
        if (StringUtils.isNotEmpty(activityStatus)) {
            if (activityStatus.equals("进行中")) {
                activityStatus = "0";
            }
            if (activityStatus.equals("未开始")) {
                activityStatus = "1";
            }
            if (activityStatus.equals("已结束")) {
                activityStatus = "2";
            }
            param.put("activityStatus", activityStatus);
        }

        // 输入 筛选时间范围
        if (StringUtils.isNotBlank(formatDate)) {
            param.put("formatDate", formatDate);
        }

        // 输入筛选的党组织 树列表
        List<PartyOrganization> treePartyOrganizationList = Lists.newArrayList();
        PartyOrganization partyOrganization = partyOrganizationMapper.selectBySerialNumber(partyOrganizationSerialNumber);
        if (!ObjectUtils.isEmpty(partyOrganization)) {
            treePartyOrganizationList.add(partyOrganization);
        }
        List<PartyOrganization> partyOrganizations = partyOrganizationMapper.selectTreeListByParentSerialNumber(partyOrganizationSerialNumber, partyOrganizationType);
        if (!ObjectUtils.isEmpty(partyOrganizations)) {
            treePartyOrganizationList.addAll(partyOrganizations);
        }
        ArrayList<String> treePartyOrganizationSerialNumberList = Lists.newArrayList();
        treePartyOrganizationList.forEach(o1 -> {
            if (o1.getPartyOrganizationType().equals(partyOrganizationType)) {
                treePartyOrganizationSerialNumberList.add(o1.getPartyOrganizationSerialNumber());
            }
        });
        param.put("treePartyOrganizationSerialNumberList", treePartyOrganizationSerialNumberList);
        PageHelper.startPage(pageNum, pageSize);
        List<PartyOrganizationActivitiesStaticVo> partyOrganizationActivitiesStaticVos = partyOrganizationActivityMapper.getMicroPartyOrganizationActivityListByStatus(param);
        partyOrganizationActivitiesStaticVos.forEach(pv ->
        {
            if (pv.getActivityStatus().equals("0")) {
                pv.setActivityStatus("进行中");
            }
            if (pv.getActivityStatus().equals("1")) {
                pv.setActivityStatus("未开始");
            }
            if (pv.getActivityStatus().equals("2")) {
                pv.setActivityStatus("已结束");
            }

        });
        PageInfo<PartyOrganizationActivitiesStaticVo> partyOrganizationActivitiesStaticVoPageInfo = new PageInfo<>(partyOrganizationActivitiesStaticVos);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("list", partyOrganizationActivitiesStaticVoPageInfo.getList());
        jsonObject.put("total", partyOrganizationActivitiesStaticVoPageInfo.getTotal());
        return jsonObject;
    }

    @Override
    public JSONObject getMicroPartyOrganizationActivitiesUploadList(
            String formatDate,
            String partyOrganizationSerialNumber,
            List<String> activityTypeList,
            String uploadStatus,
            String partyOrganizationType,
            String phone,
            int pageNum,
            int pageSize
    ) {
        HashMap<String, Object> param = Maps.newHashMap();
        // 输入 活动类型名称
        param.put("phone", phone);
        param.put("activityTypeList", activityTypeList);
        if (StringUtils.isNotEmpty(uploadStatus)) {
            param.put("uploadStatus", uploadStatus);
        }

        // 输入 筛选时间范围
        if (StringUtils.isNotBlank(formatDate)) {
            param.put("formatDate", formatDate);
        }

        // 输入筛选的党组织 树列表
        List<PartyOrganization> treePartyOrganizationList = Lists.newArrayList();
        PartyOrganization partyOrganization = partyOrganizationMapper.selectBySerialNumber(partyOrganizationSerialNumber);
        if (!ObjectUtils.isEmpty(partyOrganization)) {
            treePartyOrganizationList.add(partyOrganization);
        }
        List<PartyOrganization> partyOrganizations = partyOrganizationMapper.selectTreeListByParentSerialNumber(partyOrganizationSerialNumber, partyOrganizationType);
        if (!ObjectUtils.isEmpty(partyOrganizations)) {
            treePartyOrganizationList.addAll(partyOrganizations);
        }
        ArrayList<String> treePartyOrganizationSerialNumberList = Lists.newArrayList();
        treePartyOrganizationList.forEach(o1 -> {
            if (o1.getPartyOrganizationType().equals(partyOrganizationType)) {
                treePartyOrganizationSerialNumberList.add(o1.getPartyOrganizationSerialNumber());
            }
        });
        param.put("treePartyOrganizationSerialNumberList", treePartyOrganizationSerialNumberList);
        PageHelper.startPage(pageNum, pageSize);
        List<PartyOrganizationActivitiesStaticVo> partyOrganizationActivitiesStaticVos = partyOrganizationActivityMapper.getMicroPartyOrganizationActivitiesUploadList(param);
        PageInfo<PartyOrganizationActivitiesStaticVo> partyOrganizationActivitiesStaticVoPageInfo = new PageInfo<>(partyOrganizationActivitiesStaticVos);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("list", partyOrganizationActivitiesStaticVoPageInfo.getList());
        jsonObject.put("total", partyOrganizationActivitiesStaticVoPageInfo.getTotal());
        return jsonObject;
    }

    @Override
    public List<JSONObject> getMicroPartyOrganizationActivitiesUploadDetail(String phone, String partyOrganizationActivitySerialNumber) {
        return partyOrganizationActivityMapper.getMicroPartyOrganizationActivitiesUploadDetail(phone, partyOrganizationActivitySerialNumber);
    }

    @Override
    public List<JSONObject> getPartyOrganizationActivitiesUploadDetail(String partyOrganizationActivitySerialNumber) {
        return partyOrganizationActivityMapper.getPartyOrganizationActivitiesUploadDetail(partyOrganizationActivitySerialNumber);
    }

    @Override
    public PartyOrganizationActivitiesStaticVo getMicroPartyOrganizationActivitiesStaticVoDetail(String partyOrganizationActivitySerialNumber) {
        return partyOrganizationActivityMapper.getMicroPartyOrganizationActivitiesStaticVoDetail(partyOrganizationActivitySerialNumber);
    }

    @Override
    public JSONObject getMicroTwoContactTwoReport(
            String formatDate,
            String partyOrganizationSerialNumber,
            List<String> activityTypeList,
            String activityStatus,
            String iDCardNumber,
            String partyOrganizationType,
            int pageNum,
            int pageSize) {
        HashMap<String, Object> param = Maps.newHashMap();
        // 输入 活动类型名称
        param.put("activityTypeList", activityTypeList);
        // 输入 活动状态
        if (StringUtils.isNotEmpty(activityStatus)) {
            param.put("activityStatus", activityStatus);
        }

        // 输入 筛选时间范围
        if (StringUtils.isNotBlank(formatDate)) {
            param.put("formatDate", formatDate);
        }
        // 输入身份证
        if (StringUtils.isNotBlank(iDCardNumber)) {
            param.put("iDCardNumber", iDCardNumber);
        }

        // 输入筛选的党组织 树列表
        List<PartyOrganization> treePartyOrganizationList = Lists.newArrayList();
        PartyOrganization partyOrganization = partyOrganizationMapper.selectBySerialNumber(partyOrganizationSerialNumber);
        if (!ObjectUtils.isEmpty(partyOrganization)) {
            treePartyOrganizationList.add(partyOrganization);
        }
        List<PartyOrganization> partyOrganizations = partyOrganizationMapper.selectTreeListByParentSerialNumber(partyOrganizationSerialNumber, partyOrganizationType);
        if (!ObjectUtils.isEmpty(partyOrganizations)) {
            treePartyOrganizationList.addAll(partyOrganizations);
        }
        ArrayList<String> treePartyOrganizationSerialNumberList = Lists.newArrayList();
        treePartyOrganizationList.forEach(o1 -> {
            if (o1.getPartyOrganizationType().equals(partyOrganizationType)) {
                treePartyOrganizationSerialNumberList.add(o1.getPartyOrganizationSerialNumber());
            }
        });
        param.put("treePartyOrganizationSerialNumberList", treePartyOrganizationSerialNumberList);
        PageHelper.startPage(pageNum, pageSize);
        List<PartyOrganizationActivitiesStaticVo> partyOrganizationActivitiesStaticVos = partyOrganizationActivityMapper.getMicroTwoContactTwoReport(param);

        List<String> signUpActivityIdList = partyOrganizationActivityMapper.getSignUpActivityList(iDCardNumber);
        for (PartyOrganizationActivitiesStaticVo partyOrganizationActivitiesStaticVo : partyOrganizationActivitiesStaticVos) {
            for (String s : signUpActivityIdList) {
                if (partyOrganizationActivitiesStaticVo.getPartyOrganizationActivitySerialNumber().equals(s)) {
                    partyOrganizationActivitiesStaticVo.setSignUpFlag(1);
                }
            }
        }
        PageInfo<PartyOrganizationActivitiesStaticVo> partyOrganizationActivitiesStaticVoPageInfo = new PageInfo<>(partyOrganizationActivitiesStaticVos);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("list", partyOrganizationActivitiesStaticVoPageInfo.getList());
        jsonObject.put("total", partyOrganizationActivitiesStaticVoPageInfo.getTotal());
        return jsonObject;
    }

    @Override
    public int microActivitySignUp(String partyOrganizationActivitySerialNumber, String iDCardNumber) {
        String microActivityisEdit = partyOrganizationActivityMapper.getMicroActivityisEdit(partyOrganizationActivitySerialNumber);
        if(StringUtils.isNotBlank(microActivityisEdit)){
            if ("0".equals(microActivityisEdit)) {
                partyOrganizationActivityMapper.updateMicroActivityisEdit(partyOrganizationActivitySerialNumber);
            }
        }
        return partyOrganizationActivityMapper.microActivitySignUp(partyOrganizationActivitySerialNumber, iDCardNumber);
    }


    @Override
    public JSONObject getMicroOrgTask(String type, String iDCardNumber, String status, String year, int pageNum,
                                      int pageSize) {
        if (StringUtils.isNotEmpty(type) && StringUtils.isNotEmpty(iDCardNumber)) {
            if ("帮扶任务".equals(type)) {
                HashMap<String, Object> param = Maps.newHashMap();
                param.put("iDCardNumber", iDCardNumber);
                param.put("status", status);
                PageHelper.startPage(pageNum, pageSize);
                List<JSONObject> microAssistanceTask = partyOrganizationActivityMapper.getMicroAssistanceTask(param);
                PageInfo<JSONObject> microAssistanceTaskPageInfo = new PageInfo<>(microAssistanceTask);

                JSONObject jsonObject = new JSONObject();
                jsonObject.put("list", microAssistanceTaskPageInfo.getList());
                jsonObject.put("total", microAssistanceTaskPageInfo.getTotal());
                return jsonObject;
            }
            if ("三会一课".equals(type)) {
                HashMap<String, Object> param = Maps.newHashMap();
                param.put("iDCardNumber", iDCardNumber);
                param.put("status", status);
                PageHelper.startPage(pageNum, pageSize);
                List<JSONObject> microAssistanceTask = partyOrganizationActivityMapper.getMicroMyThreeQuotaAndOneLectureTask(param);
                PageInfo<JSONObject> microAssistanceTaskPageInfo = new PageInfo<>(microAssistanceTask);
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("list", microAssistanceTaskPageInfo.getList());
                jsonObject.put("total", microAssistanceTaskPageInfo.getTotal());
                return jsonObject;
            }
            if ("双联系双报到".equals(type)) {
                HashMap<String, Object> param = Maps.newHashMap();
                param.put("iDCardNumber", iDCardNumber);
                param.put("status", status);
                PageHelper.startPage(pageNum, pageSize);
                List<JSONObject> microAssistanceTask = partyOrganizationActivityMapper.getMicroMyTwoContactTwoReportTask(param);
                PageInfo<JSONObject> microAssistanceTaskPageInfo = new PageInfo<>(microAssistanceTask);
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("list", microAssistanceTaskPageInfo.getList());
                jsonObject.put("total", microAssistanceTaskPageInfo.getTotal());
                return jsonObject;
            }

            if ("主题党日".equals(type)) {
                HashMap<String, Object> param = Maps.newHashMap();
                param.put("iDCardNumber", iDCardNumber);
                param.put("status", status);
                PageHelper.startPage(pageNum, pageSize);
                List<JSONObject> microAssistanceTask = partyOrganizationActivityMapper.getMicroMyTopicPartyDayTask(param);
                PageInfo<JSONObject> microAssistanceTaskPageInfo = new PageInfo<>(microAssistanceTask);
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("list", microAssistanceTaskPageInfo.getList());
                jsonObject.put("total", microAssistanceTaskPageInfo.getTotal());
                return jsonObject;
            }

            if ("组织生活会".equals(type)) {
                HashMap<String, Object> param = Maps.newHashMap();
                param.put("iDCardNumber", iDCardNumber);
                param.put("status", status);
                PageHelper.startPage(pageNum, pageSize);
                List<JSONObject> microAssistanceTask = partyOrganizationActivityMapper.getMicroMyOrganizationLifeTask(param);
                PageInfo<JSONObject> microAssistanceTaskPageInfo = new PageInfo<>(microAssistanceTask);
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("list", microAssistanceTaskPageInfo.getList());
                jsonObject.put("total", microAssistanceTaskPageInfo.getTotal());
                return jsonObject;
            }

            if ("民主生活会".equals(type)) {
                HashMap<String, Object> param = Maps.newHashMap();
                param.put("iDCardNumber", iDCardNumber);
                param.put("status", status);
                PageHelper.startPage(pageNum, pageSize);
                List<JSONObject> microAssistanceTask = partyOrganizationActivityMapper.getMicroMyDemocracyLifeTask(param);
                PageInfo<JSONObject> microAssistanceTaskPageInfo = new PageInfo<>(microAssistanceTask);
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("list", microAssistanceTaskPageInfo.getList());
                jsonObject.put("total", microAssistanceTaskPageInfo.getTotal());
                return jsonObject;
            }


            if ("党费缴纳".equals(type)) {
                HashMap<String, Object> param = Maps.newHashMap();
                param.put("iDCardNumber", iDCardNumber);
                param.put("status", status);
                if (StringUtils.isNotEmpty(year)) {
                    param.put("year", year);
                } else {
                    param.put("year", DateFormatUtils.format(new Date(), "yyyy"));
                }
                PageHelper.startPage(pageNum, pageSize);
                List<JSONObject> microAssistanceTask = partyOrganizationActivityMapper.getMicroMyPartyFeeTask(param);
                PageInfo<JSONObject> microAssistanceTaskPageInfo = new PageInfo<>(microAssistanceTask);
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("list", microAssistanceTaskPageInfo.getList());
                jsonObject.put("total", microAssistanceTaskPageInfo.getTotal());
                return jsonObject;
            }

        } else {
            HashMap<String, Object> param = Maps.newHashMap();
            param.put("iDCardNumber", iDCardNumber);
            param.put("status", status);
            if (StringUtils.isNotEmpty(year)) {
                param.put("year", year);
            } else {
                param.put("year", DateFormatUtils.format(new Date(), "yyyy"));
            }
            PageHelper.startPage(pageNum, pageSize);
            List<JSONObject> microAssistanceTask = partyOrganizationActivityMapper.getMicroAssistanceTaskAll(param);
            PageInfo<JSONObject> microAssistanceTaskPageInfo = new PageInfo<>(microAssistanceTask);

            JSONObject jsonObject = new JSONObject();
            jsonObject.put("list", microAssistanceTaskPageInfo.getList());
            jsonObject.put("total", microAssistanceTaskPageInfo.getTotal());
            return jsonObject;
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("list", Lists.newArrayList());
        jsonObject.put("total", 0);
        return jsonObject;
    }

    @Override
    public PartyMembersAssistance getMicroOrgTaskDetail(String type, String id) {
        if (StringUtils.isNotEmpty(type) && StringUtils.isNotEmpty(id)) {
            if ("帮扶任务".equals(type)) {
                PartyMembersAssistance partyMembersAssistance =  partyOrganizationActivityMapper.getMicroAssistanceById(id);
                if (!ObjectUtils.isEmpty(partyMembersAssistance)) {
                    partyMembersAssistance.setAssistanceLogs(partyOrganizationActivityMapper.getMicroAssistanceTaskLog(partyMembersAssistance.getAssistanceSerialNumber()));
                    if (!ObjectUtils.isEmpty(partyMembersAssistance.getAssistanceLogs())) {
                        partyMembersAssistance.getAssistanceLogs().forEach(partyMembersAssistanceLog -> {
                            partyMembersAssistanceLog.put("assistanceLogsReturnVisits",partyOrganizationActivityMapper.getMicroAssistanceTaskLogReturnVisit(partyMembersAssistanceLog.getString("Assistance_Log_SerialNumber")));
                        });
                    }
                }
                return partyMembersAssistance;
            }
        }
        return new PartyMembersAssistance();
    }

    @Override
    public JSONObject getMicroAssistanceLog(String assistanceSerialNumber, int pageNum,
                                            int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<PartyMembersAssistanceLog> microPartyMembersAssistanceLogByAssistanceId = partyOrganizationActivityMapper.getMicroPartyMembersAssistanceLogByAssistanceId(assistanceSerialNumber);
        PageInfo<PartyMembersAssistanceLog> microPartyMembersAssistanceLogPageInfo = new PageInfo<>(microPartyMembersAssistanceLogByAssistanceId);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("list", microPartyMembersAssistanceLogPageInfo.getList());
        jsonObject.put("total", microPartyMembersAssistanceLogPageInfo.getTotal());
        return jsonObject;
    }

    @Override
    public JSONObject getMicroAssistanceReturn(String assistanceLogSerialNumber, int pageNum,
                                               int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<PartyMembersAssistanceReturnVisit> microPartyMembersAssistanceReturnVisitByLogId = partyOrganizationActivityMapper.getMicroPartyMembersAssistanceReturnVisitByLogId(assistanceLogSerialNumber);
        PageInfo<PartyMembersAssistanceReturnVisit> microPartyMembersAssistanceReturnVisiPageInfo = new PageInfo<>(microPartyMembersAssistanceReturnVisitByLogId);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("list", microPartyMembersAssistanceReturnVisiPageInfo.getList());
        jsonObject.put("total", microPartyMembersAssistanceReturnVisiPageInfo.getTotal());
        return jsonObject;
    }

    @Override
    public JSONObject getWorkTeams(String iDCardNumber, String workTeamSerialNumber) {
        JSONObject jsonObject = new JSONObject();
        WorkTeams workTeamsMyInvolved;
        if (StringUtils.isNotEmpty(workTeamSerialNumber)) {
            workTeamsMyInvolved = workTeamsMapper.getWorkTeamsByWorkTeamSerialNumber(workTeamSerialNumber);
        } else {
            workTeamsMyInvolved = workTeamsMapper.getWorkTeamsMyInvolved(iDCardNumber);
        }
        if (!ObjectUtils.isEmpty(workTeamsMyInvolved)) {
            List<WorkTeamsMembers> workTeamsMembersByTeamSerialNumber = workTeamsMapper.getWorkTeamsMembersByTeamSerialNumber(workTeamsMyInvolved.getWorkTeamSerialNumber());
            List<WorkTeamsMembers> dillWorkTeams = Lists.newArrayList();
            for (WorkTeamsMembers m : workTeamsMembersByTeamSerialNumber) {
                if (!m.getiDCardNumber().equals(workTeamsMyInvolved.getCaptainNumber())) {
                    m.setVillageName(workTeamsMyInvolved.getVillageName());
                    dillWorkTeams.add(m);
                }
                // 返回当前登陆人的队员编码 用于写日志的时候使用
                if (m.getiDCardNumber().equals(iDCardNumber)) {
                    workTeamsMyInvolved.setMyWorkTeamsMemberSerialNumber(m.getWorkTeamMemberSerialNumber());
                }
                if("1".equals(m.getIsCaptain())){
                    workTeamsMyInvolved.setMobile(m.getMobile());
                }

            }
            jsonObject.put("workTeamsMembers", dillWorkTeams);
            jsonObject.put("workTeams", workTeamsMyInvolved);
        } else {
            jsonObject.put("workTeamsMembers", Lists.newArrayList());
            jsonObject.put("workTeams", new WorkTeams());
        }
        return jsonObject;
    }

    @Override
    public JSONObject getWorkTeamsLogs(String yearMonth, String iDCardNumber, String workTeamSerialNumber) {
        JSONObject jsonObject = new JSONObject();
        WorkTeams workTeams = workTeamsMapper.getWorkTeamsByWorkTeamSerialNumber(workTeamSerialNumber);
        String ifCaptain = "0";
        if (workTeams.getCaptainNumber().equals(iDCardNumber)) {
            ifCaptain = "1";
        }
        //如果是队长能看所有 队员能看自己的日志
        List<WorkTeamsWorksLogs> workTeamsLogsByTeamSerialNumber = workTeamsMapper.getWorkTeamsLogsByTeamSerialNumber(yearMonth, workTeamSerialNumber, iDCardNumber, ifCaptain);
        if (!ObjectUtils.isEmpty(workTeamsLogsByTeamSerialNumber)) {
            workTeamsLogsByTeamSerialNumber.forEach(m -> {
                String logDate = m.getLogDate();
                m.setLogTitle(logDate + " 工作日志");
                m.setWorkTeamName(workTeams.getWorkTeamName());
            });
            jsonObject.put("workTeamsLogs", workTeamsLogsByTeamSerialNumber);
        } else {
            jsonObject.put("workTeamsLogs", Lists.newArrayList());
        }
        return jsonObject;
    }

    @Override
    public JSONObject getMicroManagerWorkTeamsList(String partyOrganizationSerialNumber, String search, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<WorkTeams> workTeams = workTeamsMapper.getMicroManagerWorkTeamsList(partyOrganizationSerialNumber, search);
        PageInfo<WorkTeams> pageInfo = new PageInfo<>(workTeams);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("list", pageInfo.getList());
        jsonObject.put("total", pageInfo.getTotal());
        return jsonObject;
    }

    @Override
    public JSONObject getMicroManagerWorkTeamsDetail(String workTeamSerialNumber) {
        JSONObject jsonObject = new JSONObject();
        WorkTeams workTeamsMyInvolved = workTeamsMapper.getWorkTeamsByWorkTeamSerialNumber(workTeamSerialNumber);
        if (!ObjectUtils.isEmpty(workTeamsMyInvolved)) {
            List<WorkTeamsMembers> workTeamsMembersByTeamSerialNumber = workTeamsMapper.getWorkTeamsMembersByTeamSerialNumber(workTeamsMyInvolved.getWorkTeamSerialNumber());
            List<WorkTeamsMembers> dillWorkTeams = Lists.newArrayList();
            workTeamsMembersByTeamSerialNumber.forEach(m -> {
                if (!m.getiDCardNumber().equals(workTeamsMyInvolved.getCaptainNumber())) {
                    m.setVillageName(workTeamsMyInvolved.getVillageName());
                    dillWorkTeams.add(m);
                }
                if("1".equals(m.getIsCaptain())){
                    workTeamsMyInvolved.setMobile(m.getMobile());
                }
            });
            jsonObject.put("workTeamsMembers", dillWorkTeams);
            jsonObject.put("workTeams", workTeamsMyInvolved);
        } else {
            jsonObject.put("workTeamsMembers", Lists.newArrayList());
            jsonObject.put("workTeams", new WorkTeams());
        }
        return jsonObject;
    }

    @Override
    public JSONObject getMicroManagerPartyOrganizationActivityList(
            String search,
            String partyOrganizationSerialNumber,
            List<String> activityTypeList,
            String activityStatus,
            String partyOrganizationType,
            String phone, int pageNum, int pageSize) {
        List<PartyOrganization> treePartyOrganizationList = Lists.newArrayList();
        String[] split = partyOrganizationSerialNumber.split(",");
        for (String s : split) {
            PartyOrganization partyOrganization = partyOrganizationMapper.selectBySerialNumber(s.substring(1, s.length() - 1));
            if (!ObjectUtils.isEmpty(partyOrganization)) {
                treePartyOrganizationList.add(partyOrganization);
            }
        }
//        PartyOrganization partyOrganization = partyOrganizationMapper.selectBySerialNumber(partyOrganizationSerialNumber);
//        if (!ObjectUtils.isEmpty(partyOrganization)) {
//            treePartyOrganizationList.add(partyOrganization);
//        }
//
        ArrayList<String> treePartyOrganizationSerialNumberList = Lists.newArrayList();
        treePartyOrganizationList.forEach(o1 -> {
            if (o1.getPartyOrganizationType().equals(partyOrganizationType)) {
                treePartyOrganizationSerialNumberList.add(o1.getPartyOrganizationSerialNumber());
            }
        });
        if (CollectionUtils.isEmpty(treePartyOrganizationSerialNumberList)) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("list", Lists.newArrayList());
            jsonObject.put("total", 0);
            return jsonObject;
        }
        HashMap<String, Object> param = Maps.newHashMap();
        param.put("treePartyOrganizationSerialNumberList", treePartyOrganizationSerialNumberList);
        param.put("activityTypeList", activityTypeList);
        param.put("search", search);
        if (StringUtils.isNotEmpty(activityStatus)) {
            if (activityStatus.equals("进行中")) {
                activityStatus = "0";
            }
            if (activityStatus.equals("未开始")) {
                activityStatus = "1";
            }
            if (activityStatus.equals("已结束")) {
                activityStatus = "2";
            }
            param.put("activityStatus", activityStatus);
        }
        param.put("phone", phone);
        PageHelper.startPage(pageNum, pageSize);
        List<PartyOrganizationActivitiesStaticVo> partyOrganizationActivitiesStaticVos = partyOrganizationActivityMapper.getMicroManagerPartyOrganizationActivityList(param);
        partyOrganizationActivitiesStaticVos.forEach(pv ->
        {
            if (pv.getActivityStatus().equals("0")) {
                pv.setActivityStatus("进行中");
            }
            if (pv.getActivityStatus().equals("1")) {
                pv.setActivityStatus("未开始");
            }
            if (pv.getActivityStatus().equals("2")) {
                pv.setActivityStatus("已结束");
            }

        });
        PageInfo<PartyOrganizationActivitiesStaticVo> partyOrganizationActivitiesStaticVoPageInfo = new PageInfo<>(partyOrganizationActivitiesStaticVos);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("list", partyOrganizationActivitiesStaticVoPageInfo.getList());
        jsonObject.put("total", partyOrganizationActivitiesStaticVoPageInfo.getTotal());
        return jsonObject;
    }

    @Override
    public JSONObject getMicroManagerOrgTask(String search, String type, String partyOrganizationSerialNumber, String phone, String status, int pageNum, int pageSize) {
        if (StringUtils.isNotEmpty(type) && StringUtils.isNotEmpty(phone)) {
            if ("帮扶任务".equals(type)) {
                HashMap<String, Object> param = Maps.newHashMap();
                param.put("phone", phone);
                param.put("status", status);
                param.put("search", search);
                param.put("partyOrganizationSerialNumber", partyOrganizationSerialNumber);
                PageHelper.startPage(pageNum, pageSize);
                List<JSONObject> microAssistanceTask = partyOrganizationActivityMapper.getMicroManagerAssistanceTask(param);
                PageInfo<JSONObject> microAssistanceTaskPageInfo = new PageInfo<>(microAssistanceTask);

                JSONObject jsonObject = new JSONObject();
                jsonObject.put("list", microAssistanceTaskPageInfo.getList());
                jsonObject.put("total", microAssistanceTaskPageInfo.getTotal());
                return jsonObject;
            }
        } else {
            // 全部中 只能查看 帮扶任务 其他的待定
            HashMap<String, Object> param = Maps.newHashMap();
            param.put("phone", phone);
            param.put("status", status);
            param.put("search", search);
            param.put("partyOrganizationSerialNumber", partyOrganizationSerialNumber);
            PageHelper.startPage(pageNum, pageSize);
            List<JSONObject> microAssistanceTask = partyOrganizationActivityMapper.getMicroManagerAssistanceTask(param);
            PageInfo<JSONObject> microAssistanceTaskPageInfo = new PageInfo<>(microAssistanceTask);

            JSONObject jsonObject = new JSONObject();
            jsonObject.put("list", microAssistanceTaskPageInfo.getList());
            jsonObject.put("total", microAssistanceTaskPageInfo.getTotal());
            return jsonObject;
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("list", Lists.newArrayList());
        jsonObject.put("total", 0);
        return jsonObject;
    }

    @Override
    public JSONObject getRandomPhoto(String phone, String status, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<JSONObject> randomPhotoList = partyOrganizationActivityMapper.getRandomPhoto(phone, status);
        PageInfo<JSONObject> randomPhotoListPageInfo = new PageInfo<>(randomPhotoList);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("list", randomPhotoListPageInfo.getList());
        jsonObject.put("total", randomPhotoListPageInfo.getTotal());
        return jsonObject;
    }

    @Override
    public JSONObject getRandomPhotoDetail(String complaintId) {
        JSONObject randomPhotoDetail = partyOrganizationActivityMapper.getRandomPhotoDetail(complaintId);
        if (ObjectUtils.isEmpty(randomPhotoDetail)) {
            return new JSONObject();
        }
        List<JSONObject> randomPhotoProcessingRecord = partyOrganizationActivityMapper.getRandomPhotoProcessingRecord(complaintId);
        randomPhotoDetail.put("record", randomPhotoProcessingRecord);
        return randomPhotoDetail;
    }

    @Override
    public JSONObject getMicroPartyOrganizationActivitiesCheckDetail(String partyOrganizationActivitySerialNumber) {
        List<JSONObject> checkInMemberList = partyOrganizationActivityMapper.getMicroPartyOrganizationActivitiesCheckDetail(partyOrganizationActivitySerialNumber);
        List<JSONObject> checkInTrue = Lists.newArrayList();
        List<JSONObject> checkInNo = Lists.newArrayList();
        for (JSONObject o1 : checkInMemberList) {
            if (o1.getString("ifCheckIn").equals("1")) {
                checkInTrue.add(o1);
            } else {
                checkInNo.add(o1);
            }
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("checkInAll", checkInMemberList);

        jsonObject.put("checkInTrue", checkInTrue);
        jsonObject.put("checkInNo", checkInNo);

        jsonObject.put("total", checkInMemberList.size());
        jsonObject.put("checkCnt", checkInTrue.size());
        return jsonObject;
    }

    @Override
    public List<JSONObject> getMicroOrgTaskTypeList() {
        List<JSONObject> jsonObjects = Lists.newArrayList();
        jsonObjects.add(new JSONObject().fluentPut("title", "全部").fluentPut("id", "0"));
        jsonObjects.add(new JSONObject().fluentPut("title", "帮扶任务").fluentPut("id", "1"));
        jsonObjects.add(new JSONObject().fluentPut("title", "三会一课").fluentPut("id", "2"));
        jsonObjects.add(new JSONObject().fluentPut("title", "双联系双报到").fluentPut("id", "3"));
        jsonObjects.add(new JSONObject().fluentPut("title", "党费缴纳").fluentPut("id", "4"));
        jsonObjects.add(new JSONObject().fluentPut("title", "组织生活会").fluentPut("id", "5"));
        jsonObjects.add(new JSONObject().fluentPut("title", "民主生活会").fluentPut("id", "6"));

        return jsonObjects;
    }

    @Override
    public JSONObject getActivityPoints(String iDCardNumber, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<JSONObject> randomPhotoList = partyOrganizationActivityMapper.getActivityPoints(iDCardNumber);
        PageInfo<JSONObject> randomPhotoListPageInfo = new PageInfo<>(randomPhotoList);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("list", randomPhotoListPageInfo.getList());
        jsonObject.put("total", randomPhotoListPageInfo.getTotal());
        return jsonObject;
    }

    @Override
    public List<JSONObject> getWorkTeamMember(String iDCardNumber, String workTeamSerialNumber) {
        return workTeamsMapper.getWorkTeamMember(iDCardNumber,workTeamSerialNumber);
    }
}

